package com.shushan.pages;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.shushan.base.NavigationLoseActivity;
import com.shushan.pages.bean.VkeBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.BuildConfig;
import com.shushan.shushanbao.R;
import com.shushan.util.DialogUtil;
import com.shushan.util.ImageUtils;
import com.shushan.util.Session;
import com.shushan.util.ToolUtils;

import java.util.HashMap;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * @ClassName: VkeDetailActivity
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-12-10
 */
public class VkeDetailActivity extends NavigationLoseActivity implements View.OnClickListener {

    private static final String TAG = "VkeDetailActivity";

    //专题名
    private TextView tvSpecialTopicTitle;
    //专题老师
    private TextView tvTeacherName;
    //科目名
    private TextView tvSubjectName;
    //专题描述
    private TextView tvSpecialTopicDes;
    //专题图片
    private ImageView ivSpecialTopicImg;
    private View timeLable;
    private ImageView ivChangeSpecialTopic;

    private JZVideoPlayerStandard jzVideoPlayerStandard;
    private Bitmap firstFrame;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            jzVideoPlayerStandard.thumbImageView.setImageBitmap(firstFrame);
        }
    };

    private int subjectId;
    private VkeBean vkeBean;
    private BabyApplication shuShanApplication = BabyApplication.getInstance();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study_with_exercise_new);

        setTitle("微课学习");

        subjectId = getIntent().getIntExtra("subjectId",0);

        vkeBean = (VkeBean) Session.getSession().get("vkeBean");

        initView();

        initData();
    }

    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }

    private void initView(){
        tvSpecialTopicTitle = findViewById(R.id.tv_special_topic_title);
        tvTeacherName = findViewById(R.id.tv_teacher_name);
        tvSubjectName = findViewById(R.id.tv_subject_name);
        tvSpecialTopicDes = findViewById(R.id.tv_special_topic_des);
        ivSpecialTopicImg = findViewById(R.id.iv_special_topic_pic);
        ivChangeSpecialTopic = findViewById(R.id.iv_change_special_topic);
        ivChangeSpecialTopic.setVisibility(View.GONE);
        timeLable = findViewById(R.id.view_3);
        jzVideoPlayerStandard = findViewById(R.id.video_view);
    }

    private void initData(){
        if(vkeBean != null){
            setFirstFrameDrawable(vkeBean.video_url);

            jzVideoPlayerStandard.setUp(vkeBean.video_url,JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,vkeBean.video_title);

            tvSpecialTopicTitle.setText(vkeBean.video_title);
            tvTeacherName.setText("讲师： " + vkeBean.video_teacher);
            tvSubjectName.setText("科目： " + ToolUtils.getSubjectNameFromId(subjectId));
            tvSpecialTopicDes.setText(vkeBean.video_desc);

            ImageUtils.loadImageView(this,vkeBean.cover,ivSpecialTopicImg);

        }else {
            showToast("信息为空");
            return;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_continue_study://我想再学学
                DialogUtil.dismissDialog();
                break;
            default:
                break;
        }

    }

    private void setFirstFrameDrawable(final String url){
        new Thread(new Runnable() {
            @Override
            public void run() {
                MediaMetadataRetriever mmr = new MediaMetadataRetriever();
                Log.d(TAG,"video_url = "+url);
                String loadUrl;
                if(ImageUtils.isAbsoluteUrl(url)){
                    loadUrl = url;
                }else{
                    loadUrl = BuildConfig.imgBaseUrl+url;
                }
                Log.d(TAG,"loadUrl = "+loadUrl);

                try {
                    mmr.setDataSource(loadUrl,new HashMap<String, String>());
                    firstFrame = mmr.getFrameAtTime();
                    mHandler.sendEmptyMessage(0);
                }catch (IllegalArgumentException ex){
                    ex.printStackTrace();
                }catch (RuntimeException ex){
                    ex.printStackTrace();
                } finally {
                    try {
                        mmr.release();
                    } catch (RuntimeException ex) {
                        ex.printStackTrace();
                    }
                }

            }
        }).start();
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }
}
